package cn.monkey.io.transport.core;

import java.util.Optional;

public record CompositeExchangeContext(ExchangeContext previous, ExchangeContext next) implements ExchangeContext {

    @Override
    public <T> T setAttribute(String key, T val) {
        T pre = this.previous.setAttribute(key, val);
        T n = this.next.setAttribute(key, pre);
        return pre == null ? n : pre;
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T> T getAttribute(String key) {
        return (T) Optional.ofNullable(this.previous.getAttribute(key))
                .orElse(this.next.getAttribute(key));
    }
}
